package com.sky.controller.admin;

import com.sky.result.Result;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("/admin/common")
public class CommonController {



    @PostMapping("/upload")
    public Result<String> upload(@RequestParam("file") MultipartFile file) {
        String projectPath = System.getProperty("user.dir");

        // Define the upload directory
        String uploadDir = "/sky-common/src/main/java/com/sky/img/";

        // Create a new file instance
        File destFile = new File(projectPath + uploadDir + file.getOriginalFilename());

        try {
            // Make sure the directory exists
            destFile.getParentFile().mkdirs();

            // Save the uploaded file to the new location
            file.transferTo(destFile);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.error("文件上传失败");
        }

        // 返回文件的路径
        return Result.success(destFile.getAbsolutePath());
    }


}
